<?php
namespace MyModel;
use MyBase\BaseModel;
class UserModel extends BaseModel{
	protected $_validate = array(
		array('name','required','用户必需填',0,'unique',1),
		array('name','checkLen','用户名长度1-12',0,'callback',1)
		);

	public function checklen($data){ 
		//验证说明  回调函数的第一个参数是系统自动给加进来的，第二个，第三个....是$_validate的第七个参数提供
		//比如array(1,2,3)那么那么回调的参数一次是  checklen($data ,1,2,3) 
	    if (strlen($data)>1||strlen($data)<12)  
	        return true;  
	    return false;  
	}  

	//根据用户名
	public function getUserInfoByUserName($userName,$flag=false){
		$where['name'] = $userName;
		if($flag){
			$where['flag'] = 'Y';
		}
		$res = $this->getRowsByCondition($where);
		if(count($res) < 1){
			return false;
		}
		return $res[0];
	}

	//根据用户邮箱获取 用户信息
	public function getUserInfoByUserEmail($userEmail,$flag=false){
		$where['email'] = $userEmail;
		if($flag){
			$where['flag'] = 'Y';
		}
		$res = $this->getRowsByCondition($where);
		if(count($res) < 1){
			return false;
		}
		return $res[0];
	}
	//根据用户Id获取用户信息
	public function getUserInfoById($id){
		return $this->where("id=$id")->find();
	}
	//检查用户名是否已经被注册
	public function checkUserNameUnique($userName){
		$where['name'] = $userName;
		$res = $this->getRowsByCondition($where);
		if(count($res) ==1){
			return true;
		}
		return false;
	}
	//检查邮箱是否已经被注册
	public function checkEmailUnique($userEmail){
		$where['email'] = $userEmail;
		$res = $this->getRowsByCondition($where);
		if(count($res) >= 1 ){
			return true;
		}
		return false;
	}
	//用户删除
	public function userDelete($id){
		$where = array();
		if(is_numeric($id)){
			$where['id'] = array('EQ',$id);
		}
		if(is_array($id)){
			$where['id'] = array('IN',$id);
		}
		return $this->where($where)->delete();
	}

}